package net.hn.hnms.biz.emgcy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.emgcy.domain.RescueTeamPo;
import net.hn.hnms.biz.emgcy.domain.dto.ResuceTeamDto;
import net.hn.hnms.biz.emgcy.domain.vo.ResuceTeamVo;
import net.hn.hnms.biz.emgcy.listener.RescueTeamListener;
import net.hn.hnms.biz.emgcy.service.RescueTeamService;
import net.hn.hnms.biz.emgcy.template.RescueTeamTemplate;
import net.hn.hnms.biz.emgcy.util.ExcelUtils;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: baijiajun
 * @Date: 2022/9/14
 */
@Api(value = "应急救援队伍", tags = {"救援队伍"})
@RestController
@RequestMapping("rescueTeam")
public class RescueTeamController {
    @Resource
    private RescueTeamService rescueTeamService;

    @Autowired
    private RemoteDictService remoteDictService;

    @ApiOperation("救援队伍列表")
    @GetMapping("pageList")
    @AutoSetMineCode
    public R<ResponsePage<ResuceTeamVo>> pageList(PageQuery pageQuery, ResuceTeamDto resuceTeamDto) {
        ResponsePage<ResuceTeamVo> list=rescueTeamService.pageList(pageQuery,resuceTeamDto);
        return R.ok(list);
    }

    @ApiOperation("救援队伍添加")
    @PostMapping("/add")
    public R add(@RequestBody RescueTeamPo rescueTeamPo){
        Boolean flag = rescueTeamService.add(rescueTeamPo);
        if(flag){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("救援队伍修改")
    @PutMapping("/update")
    public R update(@RequestBody RescueTeamPo rescueTeamPo){
        Boolean flag = rescueTeamService.update(rescueTeamPo);
        if(flag){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
    @ApiOperation("救援删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        Boolean flag = rescueTeamService.delete(id);
        if(flag){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation(value = "救援队伍列表列表导出", position = 1)
    @GetMapping("export")
    public void export(HttpServletResponse httpServletResponse) throws IOException {
        rescueTeamService.export(httpServletResponse);
    }

    /**
     * 导出模板
     */
    @GetMapping(value = "exportTemplate")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        List<RescueTeamTemplate> list = new ArrayList<>();
        ExcelUtils.exportExcel(response, "救援队伍导出模板", RescueTeamTemplate.class, list);
    }

    /**
     * 导入
     */
    @PostMapping(value = "import")
    public void testImport(MultipartFile file) {
        RescueTeamListener rescueTeamListener = new RescueTeamListener(rescueTeamService,remoteDictService);
        ExcelUtils.importExcel(file, RescueTeamTemplate.class, rescueTeamListener);
    }
}
